﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class Admin_Class : System.Web.UI.Page
{
    ILE_Config ileConfig = new ILE_Config();
    ILE_Global ileGlobal = new ILE_Global();

    ClassBLL classLogic = new ClassBLL();
    ClassStandardsBLL classStandardsLogic = new ClassStandardsBLL();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            lstboxSections.Items.Clear();

            for (int i = 1; ileConfig.getMaximumSectionNumber() >= i; i++)
            {
                ListItem newItem = new ListItem();
                newItem.Text = i.ToString();
                newItem.Value = i.ToString();
                lstboxSections.Items.Add(newItem);
            }
        }
    }

    protected void InitializePage()
    {
        GridViewClasses.SelectedIndex = -1;
        AccordionEditClass.Visible = false;
    }

    protected void btnOpenAddClass_Click(object sender, EventArgs e)
    {
        if (MultiViewClass.Visible == false)
        {
            MultiViewClass.Visible = true;
        }

        MultiViewClass.ActiveViewIndex = 0;
    }

    protected void btnOpenEditClass_Click(object sender, EventArgs e)
    {
        if (MultiViewClass.Visible == false)
        {
            MultiViewClass.Visible = true;
        }

        MultiViewClass.ActiveViewIndex = 1;
        InitializePage();
        GridViewClasses.Sort("CourseID", SortDirection.Descending);
    }

    protected void btnAddClass_Click(object sender, EventArgs e)
    {
        bool result = true;
        ClassBLL classLogic = new ClassBLL();
        int CourseID = 0;
        string ClassTitle = ileGlobal.Validated(txtClassTitle.Text);
        string ClassSubtitle = ileGlobal.Validated(txtClassSubtitle.Text);

        string Introduction = txtIntroduction.Value;
        bool IsEnabled = chkboxIsEnabled.Checked;

        try
        {
            CourseID = classLogic.InsertCourse(ClassTitle, ClassSubtitle, Introduction, IsEnabled);
        }
        catch (Exception ex1)
        {
            ileGlobal.RecordError("Class.aspx.cs:btnAddClass_Click:001", ex1, 3, Request.Browser);
            result = false;
        }

        if (CourseID != 0)
        {
            try
            {
                foreach (ListItem section in lstboxSections.Items)
                {
                    if (section.Selected)
                    {
                        classLogic.InsertCourseSection(CourseID, Convert.ToInt32(section.Value), false, "", "", "", true);
                    }
                }
            }
            catch (Exception ex2)
            {
                ileGlobal.RecordError("Class.aspx.cs:btnAddClass_Click:002", ex2, 3, Request.Browser);
                result = false;
            }
        }
        else
        {
            ileGlobal.RecordError("Class.aspx.cs:btnAddClass_Click:003", "CourseID was 0", 3, Request.Browser);
            result = false;
        }

        if (result == true)
        {
            string modalPopupMsg = "Your class has been successfully created.";
            ModalPopupMessage.ShowModalPopup("ILE Message", modalPopupMsg, 1);


            for (int StandardID = 1; StandardID <= 4; StandardID++)
            {
                if (!classStandardsLogic.IsDuplicated(CourseID, StandardID))
                {
                    classStandardsLogic.InsertClassStandard(CourseID, StandardID, true);
                }
            }

            lstboxSections.SelectedIndex = -1;
            txtClassTitle.Text = "";
            txtClassSubtitle.Text = "";
            txtIntroduction.Value = "";
        }
        else
        {
            ModalPopupMessage.ShowModalPopup("ILE Message", "Failed to create your class. :(", 2);
        }
    }

    protected void GridViewClasses_SelectedIndexChanged(object sender, EventArgs e)
    {
        AccordionEditClass.SelectedIndex = 0;
        AccordionEditClass.Visible = true;
        LoadEditClassDefaultInformation();
        GridViewEditSectionList.SelectedIndex = -1;
        PanelEditSection.Visible = false;

        UpdateAddOrRemoveSectionPanel();
    }

    protected void UpdateAddOrRemoveSectionPanel()
    {
        int CourseID = Convert.ToInt32(GridViewClasses.SelectedValue);

        lstboxAvailableSections.Items.Clear();
        lstboxCurrentSections.Items.Clear();

        Classes.Courses_SectionDataTable sections = classLogic.GetSectionsByCourseID(CourseID, false);

        for (int i = 1; i <= ileConfig.getMaximumSectionNumber(); i++)
        {
            bool isDuplicate = false;

            foreach (Classes.Courses_SectionRow section in sections)
            {
                if (section.SectionNumber == i)
                {
                    isDuplicate = true;
                    break;
                }
            }

            if (isDuplicate == false)
            {
                ListItem newItem = new ListItem(i.ToString(), i.ToString());
                lstboxAvailableSections.Items.Add(newItem);
            }
        }

        foreach (Classes.Courses_SectionRow section in sections)
        {
            ListItem newListItem = new ListItem(section.SectionNumber.ToString(), section.SectionNumber.ToString());
            lstboxCurrentSections.Items.Add(newListItem);
        }
    }

    protected void btnEditClass_Click(object sender, EventArgs e)
    {
        // UPDATE CLASS DEFAULT INFORMATION
        string Title = txtEditClassTitle.Text;
        string Subtitle = txtEditClassSubtitle.Text;
        string Introduction = txtEditIntroduction.Value;
        bool IsEnabled = chkEditIsEnabled.Checked;

        bool result = classLogic.UpdateCourse(Title, Subtitle, Introduction, IsEnabled, Convert.ToInt32(GridViewClasses.SelectedValue));
        if (result)
        {
            string modalPopupMsg = "Your class has been successfully edited.";
            ModalPopupMessage.ShowModalPopup("ILE Message", modalPopupMsg, 1);

            GridViewClasses.DataBind();
        }
        else
        {
            string modalPopupMsg = "Failed to edit the class information.";
            ModalPopupMessage.ShowModalPopup("ILE Message", modalPopupMsg, 2);
        }
    }

    protected void GridViewEditSectionList_SelectedIndexChanged(object sender, EventArgs e)
    {
        LoadEditSectionInformation();
        PanelEditSection.Visible = true;
    }

    protected void LoadEditClassDefaultInformation()
    {
        Classes.CoursesRow course = classLogic.GetCourse(Convert.ToInt32(GridViewClasses.SelectedValue));

        txtEditClassTitle.Text = course.Title;
        txtEditClassSubtitle.Text = course.Subtitle;
        txtEditIntroduction.Value = course.Introduction;
        chkEditIsEnabled.Checked = course.IsEnabled;
    }

    protected void LoadEditSectionInformation()
    {
        int CourseID = Convert.ToInt32(GridViewClasses.SelectedValue);
        int SectionNumber = Convert.ToInt32(GridViewEditSectionList.SelectedDataKey.Values[1]);

        Classes.Courses_SectionRow section = classLogic.GetSection(CourseID, SectionNumber);

        chkEditUseSectionInfo.Checked = section.UseSectionInfo;
        txtEditSectionSubtitle.Text = section.Subtitle;
        txtEditSectionClassTime.Text = section.ClassTime;
        txtEditSectionIntroduction.Value = section.Introduction;
        chkEditSectionIsEnabled.Checked = section.IsEnabled;
    }

    protected void btnEditSection_Click(object sender, EventArgs e)
    {
        // UPDATE Section INFORMATION
        int CourseID = Convert.ToInt32(GridViewClasses.SelectedValue);
        int SectionNumber = Convert.ToInt32(GridViewEditSectionList.SelectedDataKey.Values[1]);

        bool UseSectionInfo = chkEditUseSectionInfo.Checked;
        string SectionSubtitle = txtEditSectionSubtitle.Text;
        string ClassTime = txtEditSectionClassTime.Text;
        string SectionIntroduction = txtEditSectionIntroduction.Value;
        bool IsSectionEnabled = chkEditSectionIsEnabled.Checked;

        bool result = classLogic.UpdateSection(UseSectionInfo, SectionSubtitle, SectionIntroduction, ClassTime, IsSectionEnabled, CourseID, SectionNumber);
        if (result)
        {
            string modalPopupMsg = "Your section has been successfully edited.";
            ModalPopupMessage.ShowModalPopup("ILE Message", modalPopupMsg, 1);

            GridViewEditSectionList.DataBind();
        }
        else
        {
            string modalPopupMsg = "Failed to edit the section information.";
            ModalPopupMessage.ShowModalPopup("ILE Message", modalPopupMsg, 2);
        }
    }

    protected void btnAddSections_Click(object sender, EventArgs e)
    {
        int CourseID = Convert.ToInt32(GridViewClasses.SelectedValue);

        foreach (ListItem item in lstboxAvailableSections.Items)
        {
            if (item.Selected)
            {
                classLogic.InsertCourseSection(CourseID, Convert.ToInt32(item.Value), false, String.Empty, String.Empty, String.Empty, true);
            }
        }

        UpdateAddOrRemoveSectionPanel();
        GridViewEditSectionList.DataBind();
        GridViewEditSectionList.SelectedIndex = -1;
    }

    protected void btnRemoveSections_Click(object sender, EventArgs e)
    {
        int CourseID = Convert.ToInt32(GridViewClasses.SelectedValue);

        foreach (ListItem item in lstboxCurrentSections.Items)
        {
            if (item.Selected)
            {
                classLogic.DeleteSection(CourseID, Convert.ToInt32(item.Value));
            }
        }

        UpdateAddOrRemoveSectionPanel();
        GridViewEditSectionList.DataBind();
        GridViewEditSectionList.SelectedIndex = -1;
    }

    protected void btnModalPopupConfirmDeleteClassOK_Click(object sender, EventArgs e)
    {
        int CourseID = Convert.ToInt32(GridViewClasses.SelectedValue);


        // Delete Assignments since it causes huge problems!
        AssignmentsBLL assignmentsLogic = new AssignmentsBLL();
        Assignments.RelationsDataTable relations = assignmentsLogic.GetRelationsByCourseId(CourseID);
        foreach (Assignments.RelationsRow relation in relations)
        {
            try
            {
                assignmentsLogic.DeleteAssignment(relation.AssignmentID);
            }
            catch (Exception ex)
            {
                // Noting needed :)
            }
        }

        bool result = classLogic.DeleteCourse(CourseID);
        if (result)
        {

            string modalPopupMsg = "Your class has been successfully deleted.";
            ModalPopupMessage.ShowModalPopup("ILE Message", modalPopupMsg, 1);

            GridViewClasses.DataBind();
            InitializePage();
        }
        else
        {
            string modalPopupMsg = "Failed to delete the class.";
            ModalPopupMessage.ShowModalPopup("ILE Message", modalPopupMsg, 2);
        }

    }
}
